package com.hvgroup.modules.sanic.web;


import com.alibaba.fastjson2.JSONObject;
import com.hvgroup.annotation.rest.AnonymousPostMapping;
import com.hvgroup.modules.sanic.domain.qo.CPReqBody;
import com.hvgroup.modules.sanic.service.ChatPlatformService;
import com.hvgroup.utils.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/chat/comm")
@Api(tags = "系统:会话管理")
public class ChatPlatformController {

    private final ChatPlatformService chatPlatformService;

    @PostMapping(value = "/{platformId}/{appId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> chatCompletion(@PathVariable Long platformId,
                                                        @PathVariable String appId,
                                                        @RequestBody JSONObject body) {
        CPReqBody queryData = CPReqBody.builder()
                .platformId(platformId)
                .levelId_2(appId)
                .userId(SecurityUtils.getCurrentUserId())
                .userName(SecurityUtils.getCurrentUsername())
                .body(body)
                .build();
        try {
            return chatPlatformService.chatCompletion(queryData);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Flux.empty();

    }

}
